import TencentCloudChat from '@tencentcloud/chat';
import TIMUploadPlugin from 'tim-upload-plugin';
import { genTestUserSig }  from '../../TUIKit/debug/GenerateTestUserSig';
//获取应用实例
const app = getApp();
  Page({
    data: {
      shareTitle: app.globalData.global.shareTitle,
      config: {
        userID: '01', // User ID
        SDKAPPID: 1600017915, // Your SDKAppID
        SECRETKEY: 'dcdc6ef64a2c4195a3974ceb163fedaaf77a49e5e391086dd4a02ec8e13b5036',
        EXPIRETIME: 604800,
      },
      conversationID: '',
  },
  onLoad(options) {
    console.log(options,'options')
    if(options.userId){
      var config = this.data.config;
      config.userID = options.userId
      this.setData({
        config: config,
      })
    }
    this.setData({
      conversationID: options.conversationID
    });
    const userSig = genTestUserSig(this.data.config).userSig;
    wx.$TUIKit = TencentCloudChat.create({
      SDKAppID: this.data.config.SDKAPPID
    });
    wx.$chat_SDKAppID = this.data.config.SDKAPPID;
    wx.$chat_userID = this.data.config.userID;
    wx.$chat_userSig = userSig;
    wx.TencentCloudChat = TencentCloudChat;
    wx.$TUIKit.registerPlugin({ 'tim-upload-plugin': TIMUploadPlugin });            
    wx.$TUIKit.login({
      userID: this.data.config.userID,
      userSig
    });
    wx.setStorage({
      key: 'currentUserID',
      data: [],
    });
    wx.$TUIKit.on(wx.TencentCloudChat.EVENT.SDK_READY, this.onSDKReady,this);
  },
  onUnload() {
    wx.$TUIKit.logout();
    wx.$TUIKit.off(wx.TencentCloudChat.EVENT.SDK_READY, this.onSDKReady,this);
  },
  onSDKReady() {
    const TUIKit = this.selectComponent('#TUIKit');
    TUIKit.init();
  },
  onShareAppMessage: function () {
    let userId = wx.getStorageSync('userId') ?? 10
    return {
      title: this.data.shareTitle,
      path: '/pages/Welcome/Welcome?scene='+userId,
    }
  },
});